package com.kk.acceleratedraw;

import android.graphics.Bitmap;

public class BitmapUtil {
    public static Bitmap bitmapToC(Bitmap bitmap) {
        if (bitmap.getConfig() != Bitmap.Config.ARGB_8888)
            throw new IllegalArgumentException("Bitmap must be in ARGB_8888 format");
        int[] pixelsIn = new int[bitmap.getWidth() * bitmap.getHeight()];
        int[] pixelsOut = new int[bitmap.getWidth() * bitmap.getHeight()];
        bitmap.getPixels(pixelsIn, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        for (int i = 0; i < pixelsOut.length; i++) {
            int A = (pixelsIn[i] >> 24) & 0xff;
            int R = (pixelsIn[i] >> 16) & 0xff;
            int G = (pixelsIn[i] >> 8) & 0xff;
            int B = pixelsIn[i] & 0xff;
            int pixel = (A << 24) | (R << 16) | (G << 8) | B;
            pixelsOut[i] = pixel;
        }
        Bitmap outbitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        outbitmap.setPixels(pixelsOut, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        return outbitmap;
    }
}